import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:xingsu/page/home/home_page.dart';
import 'package:xingsu/page/home/viewModel/game_search_viewmodel.dart';
import 'package:xingsu/page/publisher/detail_page.dart';
import 'package:xingsu/page/publisher/new_more_page.dart';
import 'package:xingsu/page/publisher/viewModel/task_search_viewmodel.dart';
import 'package:xingsu/page/video/model/Video_model.dart';
import 'package:xingsu/util/image_util.dart';
import 'package:xingsu/widget/back_widget.dart';
import 'package:xingsu/widget/search_widget.dart';

import '../../res/colors.dart';

class GameSearchPage extends StatelessWidget {
  final List<VideoModel> allList;

  GameSearchPage(this.allList)
      : _viewModel = Get.put(GameSearchViewModel(allList));

  final GameSearchViewModel _viewModel;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 0,
      ),
      body: Column(
        children: [
          _topWidget(),
          Expanded(
              child: Stack(
            children: [
              Obx(() => ListView.separated(
                    separatorBuilder: (context, index) {
                      return index == _viewModel.list.length - 1
                          ? const Divider(
                              height: 0,
                            )
                          : Container(
                              color: context.isDarkMode
                                  ? const Color(0xFF050505)
                                  : const Color(0xFFF5F5F5),
                              height: 8,
                            );
                    },
                    itemCount: _viewModel.list.length,
                    itemBuilder: (BuildContext context, int index) {
                      return homeItemWidget(
                          index, -1, _viewModel.list.cast<VideoModel>());
                    },
                  )),
              Obx(() =>
                  _viewModel.hasFocus.value && _viewModel.historys.isNotEmpty
                      ? Container(
                          color: Get.theme.scaffoldBackgroundColor,
                          padding: const EdgeInsets.symmetric(horizontal: 15),
                          child: ListView.builder(
                            itemCount: _viewModel.historys.length,
                            itemBuilder: (context, index) {
                              return _historyItem(_viewModel.historys[index]);
                            },
                          ),
                        )
                      : const SizedBox())
            ],
          ))
        ],
      ),
    );
  }

  Widget _historyItem(String history) {
    return InkWell(
      onTap: () {
        _viewModel.controller.text = history;
        _viewModel.search();
      },
      child: Container(
        height: 40,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    color: Get.theme.dividerTheme.color!, width: 0.8))),
        alignment: Alignment.centerLeft,
        child: Row(
          children: [
            loadAssetImage("icon_time", width: 16),
            const SizedBox(
              width: 10,
            ),
            Text(
              history,
              style: const TextStyle(color: Colours.grayColor, fontSize: 14),
            ),
            const Spacer(),
            IconButton(
                onPressed: () {
                  _viewModel.delHistory(history);
                },
                icon: const Icon(
                  Icons.close,
                  size: 12,
                ))
          ],
        ),
      ),
    );
  }

  Widget _topWidget() {
    return Row(
      children: [
        const SizedBox(
          width: 10,
        ),
        IconButton(
            onPressed: () {
              Get.back();
            },
            icon: const Icon(
              Icons.arrow_back_ios,
              size: 24,
            )),
        const SizedBox(
          width: 8,
        ),
        Expanded(
            child: SearchWidget(
                placeholder: "大家都在搜",
                controller: _viewModel.controller,
                focusNode: _viewModel.focusNode,
                onSubmitted: (str) {
                  _viewModel.search();
                })),
        const SizedBox(
          width: 10,
        ),
        InkWell(
          onTap: _viewModel.search,
          child: const Text(
            "搜索",
            style: TextStyle(color: Colours.app_main, fontSize: 14),
          ),
        ),
        const SizedBox(
          width: 10,
        ),
      ],
    );
  }
}
